<template>
  <div class="login-wrap">
    <el-form label-position="left" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm login-container">
      <h3 class="title">用户登录</h3>
      <el-form-item prop="userName">
        <el-input type="text" v-model="ruleForm.userName" auto-complete="off" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item prop="passWord">
        <el-input v-model="ruleForm.passWord" auto-complete="off" placeholder="密码"
                  :type="pwdObj.pwdType">
          <i slot="suffix" title="显示密码" @click="changePass('show')" style="cursor:pointer;"
             class="el-input__icon iconfont icon-xianshikejian"
             v-if="visible"></i>
          <i slot="suffix" title="隐藏密码" @click="changePass('hide')" style="cursor:pointer;"
             class="el-input__icon iconfont icon-bukejian"
             v-else></i>
        </el-input>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item prop="code">
            <el-input type="text"
                      v-model="ruleForm.code"
                      auto-complete="off"
                      placeholder="图形验证码">

            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="code-box">
          <img :src="ruleForm.codeImg" alt="" class="code-img" @click="initImgCode()">
        </el-col>
      </el-row>
      <div class="between-box remember">
        <el-checkbox class="" v-model="rememberpwd">记住密码</el-checkbox>
<!--        <div class="color66b curPer"-->
<!--             @click="editFormVisible = true">-->
<!--          忘记密码?-->
<!--        </div>-->
      </div>
      <el-form-item style="width:100%;">
        <el-button type="primary" style="width:100%;"
                   @click="submitForm('ruleForm')"
                   :loading="logining">
          登录
        </el-button>
      </el-form-item>
    </el-form>

    <!-- 忘记密码编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%"
               @close='closeDialog("edit")'>
      <el-form label-width="100px" :model="editForm" ref="editForm" :rules="rulesEidt">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="editForm.userName" auto-complete="off" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email" auto-complete="off" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label="邮箱验证码" prop="code">
          <el-input v-model="editForm.code" auto-complete="off" placeholder="请输入邮箱验证码"></el-input>
        </el-form-item>
        <el-form-item label="" prop=""
                      v-if="!pwdVisible">
          <code-item :showGetting="true"
                     :info="editForm">
          </code-item>
        </el-form-item>
        <el-form-item label="新密码" prop="passWord"
                      v-if="pwdVisible">
          <el-input v-model="editForm.passWord" auto-complete="off" placeholder="请输入新密码"
                    :type="pwdObjEdit.pwdType">
            <i slot="suffix" title="显示密码" @click="changePassEdit('show')" style="cursor:pointer;"
               class="el-input__icon iconfont icon-xianshikejian"
               v-if="visibleEdit"></i>
            <i slot="suffix" title="隐藏密码" @click="changePassEdit('hide')" style="cursor:pointer;"
               class="el-input__icon iconfont icon-bukejian"
               v-else></i>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer"
           v-if="pwdVisible">
        <el-button @click='closeDialog("edit")'>取消</el-button>
        <el-button type="primary" class="title" @click="submitEditForm('editForm')">保存</el-button>
      </div>
      <div slot="footer" class="dialog-footer"
           v-else>
        <el-button type="primary" class="title" @click="nextStep('editForm')">下一步</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script type="text/ecmascript-6">
import { login } from '../api/userMG'
import { setCookie, getCookie, delCookie } from '../utils/util'
import md5 from 'js-md5'
import config from "@/api/config.js";
import CodeItem from "@/components/code-item";
export default {
  name: 'login',
  components: {
    CodeItem
  },
  data() {
    return {
      //定义loading默认为false
      logining: false,
      // 记住密码
      rememberpwd: false,
      ruleForm: {
        //userName和passWord默认为空
        userName: '',
        passWord: '',
        code: '',
        randomStr: '',
        codeImg: '',
      },
      //rules前端验证
      rules: {
        userName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        passWord: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      },
      visible: true,
      pwdObj: { pwdType: 'password'},
      editFormVisible: false, //控制编辑页面显示与隐藏
      pwdVisible: false, //控制新密码显示与隐藏
      title: '忘记密码',
      editForm: {
        userName: '',
        passWord: '',
        email: '',
        code: ''
      },
      rulesEidt: {
        userName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        passWord: [{ required: true, message: '请输入新密码', trigger: 'blur' }],
        email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
        code: [{ required: true, message: '请输入邮箱验证码', trigger: 'blur' }],
      },
      pwdObjEdit: { pwdType: 'password'},
      visibleEdit: false,
    }
  },
  // 创建完毕状态(里面是操作)
  created() {
    // 获取图形验证码
    this.initImgCode()
    // 获取存在本地的用户名密码
    this.getuserpwd()

  },
  // 里面的函数只有调用才会执行
  methods: {
    changePass(value) {
      if(!this.ruleForm.passWord) return
      this.pwdObj.pwdType = value == 'show' ? 'text' : 'password'
      this.visible = !(value === 'show');
    },
    // 获取图形验证码
    initImgCode() {
      const vm = this
      vm.ruleForm.randomStr = new Date().getTime();
      // vm.ruleForm.codeImg = config.baseUrl + 'api/user/memberVeryCode?key=' + vm.ruleForm.randomStr;
      vm.ruleForm.codeImg = config.baseUrl + 'api/user/memberVeryCode?s=captcha.html&key=' + vm.ruleForm.randomStr;
      vm.code = '';
    },
    // 获取用户名密码
    getuserpwd() {
      if (getCookie('user') != '' && getCookie('pwd') != '') {
        this.ruleForm.userName = getCookie('user')
        this.ruleForm.passWord = getCookie('pwd')
        this.rememberpwd = true
      }
    },
    //获取info列表
    submitForm(editData) {
      const vm = this
      const param = {
        ...vm.ruleForm,
        "passWord": vm.$md5(vm.ruleForm.passWord)
      }
      vm.$refs[editData].validate(valid => {
        if (valid) {
          // 注释
          vm.openFullScreen();
          this.apiUrl.login(param,"POST").then(res => {
            vm.closeFullScreen(vm.openFullScreen());
            const { code ,msg, data, token} = res;
            if (code == 0) {
              if (this.rememberpwd) {
                //保存帐号到cookie，有效期7天
                setCookie('user', this.ruleForm.userName, 7)
                //保存密码到cookie，有效期7天
                setCookie('pwd', this.ruleForm.passWord, 7)
              } else {
                delCookie('user')
                delCookie('pwd')
              }
              vm.showMessageMin({
                type: 'success',
                message: '登录成功'
              })
              //如果请求成功就让他2秒跳转路由
              setTimeout(() => {
                // this.logining = false
                this.$store.commit('saveMember', data)
                this.$router.push({ path: '/feedback/feedbackList' })
              }, 2000)
            } else {
              this.$message.error(msg)
              // 获取图形验证码
              this.initImgCode()
              vm.ruleForm.code = ''
              // this.logining = false
              return false
            }
          })
        } else {
          // 获取图形验证码
          // this.initImgCode()
          // this.logining = false
          return false
        }
      })
    },
    // 关闭编辑、增加弹出框
    closeDialog(dialog) {
      const vm = this;
      if (dialog == 'edit') {
        vm.editFormVisible = false
        vm.pwdVisible = false
        vm.editForm = {
          ...vm.editForm,
          passWord: ''
        }
      }
    },
    // 编辑、增加页面保存方法
    submitEditForm(editData) {
      const vm = this;
      vm.$refs[editData].validate(valid => {
        if (valid) {
          vm.openFullScreen();
          let request = vm.apiUrl.changePsw(vm.editForm,"POST")
          request.then(res => {
            vm.closeFullScreen(vm.openFullScreen());
            vm.editFormVisible = false
            const { code,msg } = res;
            if (code != 0) {
              vm.showMessageMin({
                type: 'error',
                message: msg
              })
            } else {
              vm.showMessageMin({
                type: 'success',
                message: '密码修改成功！'
              })
              vm.closeDialog('edit')
            }
          })
        } else {
          return false
        }
      })
    },
    nextStep(editData){
      const vm = this
      vm.$refs[editData].validate(valid => {
        if (valid) {
          vm.pwdVisible = true
        }
      })
    },
    changePassEdit(value) {
      this.pwdObjEdit.pwdType = value == 'show' ? 'text' : 'password'
      this.visibleEdit = !(value === 'show');
    },

  }
}
</script>

<style scoped>
.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding-top: 10%;
  background-image: url();
  /* background-color: #112346; */
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}
.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}
.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
.remember {
  margin: 0px 0px 35px 0px;
}
.code-box {
  text-align: right;
}
.code-img {
  height: 40px;
}
</style>
